package xyz.penghaonan.p2plib.helper.message;

import android.location.Location;
import android.location.LocationManager;

import java.util.Date;

public class LocationInfoRequestMsg extends CmdRequestMsg {

    private LocationCompat location;

    public void setLocation(Location location) {
        this.location = LocationCompat.create(location);
    }

    public Location getLocation() {
        if (location == null) {
            return null;
        }
        return location.convert();
    }

    @Override
    public int getType() {
        return MsgFactory.TYPE_LOCATION_INFO_REQUEST;
    }

    public static final class LocationCompat {
        private double mLatitude = 0.0;
        private double mLongitude = 0.0;
        private double mAltitude = 0.0f;
        private float mSpeed = 0.0f;
        private float mBearing = 0.0f;
        private float mHorizontalAccuracyMeters = 0.0f;

        public static LocationCompat create(Location location) {
            if (location == null) {
                return null;
            }
            LocationCompat locationCompat = new LocationCompat();
            locationCompat.mLatitude = location.getLatitude();
            locationCompat.mLongitude = location.getLongitude();
            locationCompat.mAltitude = location.getAltitude();
            locationCompat.mSpeed = location.getSpeed();
            locationCompat.mBearing = location.getBearing();
            locationCompat.mHorizontalAccuracyMeters = location.getAccuracy();
            return locationCompat;
        }

        public Location convert() {
            String providerStr = LocationManager.GPS_PROVIDER;
            Location mockLocation = new Location(providerStr);
            mockLocation.setLatitude(mLatitude);   // 维度（度）
            mockLocation.setLongitude(mLongitude);  // 经度（度）
            mockLocation.setAltitude(mAltitude);    // 高程（米）
            mockLocation.setBearing(mBearing);   // 方向（度）
            mockLocation.setSpeed(mSpeed);    //速度（米/秒）
            mockLocation.setAccuracy(mHorizontalAccuracyMeters);   // 精度（米）
            mockLocation.setTime(new Date().getTime());   // 本地时间
            return mockLocation;
        }
    }
}
